<!-- 解决layer.open 不居中问题   -->
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>添加用户</title>
    <!--引入抽取css文件-->
    <%@include file="../common/public-css.jsp" %>
    <style>
        .preview{height: 250px;width: 400px;margin-right: 10px;margin-bottom: 10px;float: left;text-align: center}
        .preview img{width: 100%;height:210px;border: 1px solid #eee;}

    </style>
</head>
<body>
<div style="margin: 15px;">
    <blockquote class="layui-elem-quote"><i class="fa fa-refresh" aria-hidden="true"></i>&nbsp;表单带有 <span class="font-red">“*”</span> 号的为必填项.</blockquote>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>添加用户</legend>
    </fieldset>
    <form class="layui-form" action="" id="formData">

        <div class="layui-form-item">
            <label class="layui-form-label">用户角色<span class="font-red">*</span></label>
            <div class="layui-input-inline" id="roleIdDiv">

            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>姓名</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="text" name="realName" id="realName" lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" maxlength="50">
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>性别</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="radio" name="gender" value="0" title="男" checked>
                <input type="radio" name="gender" value="1" title="女" >
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>电话</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="text" name="phone" id="phone" lay-verify="required|isPhone" placeholder="请输入电话" autocomplete="off" class="layui-input" maxlength="12">
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>选择公司</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <select  lay-filter="companyFilter" id="companyId" lay-search>
                    <option value="">请选择</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item ">
            <label class="layui-form-label"><span>所属部门</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <select name="departmentId" id="departmentId"  lay-verify="required" lay-search>
                    <option value="">请选择</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>职位</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="text" name="job" id="job" lay-verify="required" placeholder="请输入工作职位" autocomplete="off" class="layui-input" maxlength="100">
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>照片</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <button type="button" class="layui-btn" id="photoUploadBtn">
                    <i class="layui-icon">&#xe608;</i>上传照片
                </button>
                <img src="" style="width: 100px;height: 100px; display: none" id="photoImg">
                <button type="button" style="display: none" class="layui-btn" id="delPhotoImgBtn">
                    <i class="layui-icon">&#xe640;</i>删除
                </button>
                <input type="hidden" name="photo" id="photo" lay-verify="required" >
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>入职日期</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="induction" readonly id="induction" lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>工号</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="text" name="jobNumber" id="jobNumber" lay-verify="required" placeholder="请输入工作手机号" autocomplete="off" class="layui-input" maxlength="50">
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>出生日期</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="text" name="birthday" id="birthday" lay-verify="required" readonly class="layui-input" >
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label"><span>民族</span><span class="font-red"></span></label>
            <div class="layui-input-inline">
                <input type="text" name="national" id="national"  placeholder="请输入民族" autocomplete="off" class="layui-input" maxlength="50">
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label"><span>学历</span><span class="font-red"></span></label>
            <div class="layui-input-inline">
                <select name="education" >
                    <option value="0">博士</option>
                    <option value="1">硕士</option>
                    <option value="2">研究生</option>
                    <option value="3">本科</option>
                    <option value="4">大专</option>
                    <option value="5">高中</option>
                    <option value="6">初中</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label"><span>住址</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <select id="province" lay-filter="provinceFilter" lay-search>
                    <option value="">选择省</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select id="city" lay-filter="cityFilter" lay-search>
                    <option value="">选择市</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select id="county" name="cityId" lay-verify="required" lay-search>
                    <option value="">选择区县</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label"><span>详细住址</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="text" name="address" id="address" lay-verify="required" placeholder="请输入详细地址" autocomplete="off" class="layui-input" maxlength="50">
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label"><span>身份证</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="text" name="idCard" id="idCard" lay-verify="required" placeholder="请输入身份证号码" autocomplete="off" class="layui-input" maxlength="50">
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label"><span>身份证照片</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <button type="button" class="layui-btn" id="idCardPositiveUploadBtn">
                    <i class="layui-icon">&#xe608;</i>上传身份证正面
                </button>
                <img src="" style="width: 300px;height: 200px; display: none" id="idCardPositiveImg">
                <button type="button" style="display: none" class="layui-btn" id="delIdCardPositiveImgBtn">
                    <i class="layui-icon">&#xe640;</i>删除
                </button>
                <input type="hidden" name="idCardPositive" lay-verify="required" id="idCardPositive" >
            </div>
            <div class="layui-input-inline">
                <button type="button" class="layui-btn" id="idCardOtherSideUploadBtn">
                    <i class="layui-icon">&#xe608;</i>上传身份证反面
                </button>
                <img src="" style="width: 300px;height: 200px; display: none" id="idCardOtherSideImg">
                <button type="button" style="display: none" class="layui-btn" id="delIdCardOtherSideImgBtn">
                    <i class="layui-icon">&#xe640;</i>删除
                </button>
                <input type="hidden" name="idCardOtherSide" lay-verify="required" id="idCardOtherSide" >
            </div>

        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label"><span>婚姻</span><span class="font-red">*</span></label>
            <div class="layui-input-inline">
                <input type="radio" name="isMarried" value="1" title="已婚">
                <input type="radio" name="isMarried" value="0" title="未婚" checked>
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label"><span>紧急联系人</span><span class="font-red"></span></label>
            <div class="layui-input-inline">
                <input type="text" name="emergencyContact" id="emergencyContact"  placeholder="请输入紧急联系人" autocomplete="off" class="layui-input" maxlength="50">
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label"><span>紧急联系电话</span><span class="font-red"></span></label>
            <div class="layui-input-inline">
                <input type="text" name="emergencyPhone" id="emergencyPhone" placeholder="请输入紧急联系电话" autocomplete="off" class="layui-input" maxlength="50">
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<!--引入抽取公共js-->
<%@include file="../common/public-js.jsp" %>
<script src="<%=request.getContextPath()%>/resources/js/common/jQuery.md5.js"></script>
<script>

    //上传的类型 0：头像 1：营业执照/介绍信
    var type = 0;
    layui.use(['form', 'layedit', 'upload', 'layedit','laydate','layer'], function() {
        var form = layui.form,
                layer = layui.layer,
                layedit = layui.layedit,
                 laydate = layui.laydate,
                upload = layui.upload,
                $ = layui.jquery;

        laydate.render({
            elem: '#induction'
        });
        laydate.render({
            elem: '#birthday'
        });

        buildRoleCheck("roleIdDiv"); //角色列表
        // 初始化公司
        buildAllCompanySelect(0,"companyId");
        // 初始化省级城市
        selectProvince(0,"province");


        form.on('select(provinceFilter)', function(data){
            selectCity(data.value,0,"city");
            form.render();
        });

        form.on('select(cityFilter)', function(data){
            selectCounty(data.value,0,"county");
            form.render();
        });

        form.on('select(companyFilter)', function(data){
            buildDepartmentSelect(data.value,0,"departmentId");
            form.render();
        });

        var uploadInst = upload.render({
            elem: '#photoUploadBtn' //绑定元素
            ,method : 'post'
            ,data : {path : 'iems'}
            ,url: Global.imageServer+'upload/images/' //上传接口
            ,done: function(res){
                //上传完毕回调
                console.log(res)
                $("#photoUploadBtn").hide("fast");
                $("#photoImg").attr("src",Global.imageServer + res.body[0].smallPath);
                $("#photoImg").show("fast");
                $("#delPhotoImgBtn").show("fast");
                $("#photo").val(res.body[0].sourcePath);
            }
            ,error: function(){
                //请求异常回调
                layer.msg("上传失败");
            }
        });

        // 上传身份证正面
        var idCardPositiveUploadBtn = upload.render({
            elem: '#idCardPositiveUploadBtn' //绑定元素
            ,method : 'post'
            ,data : {path : 'iems'}
            ,url: Global.imageServer+'upload/images/' //上传接口
            ,done: function(res){
                //上传完毕回调
                $("#idCardPositiveUploadBtn").hide();
                $("#idCardPositiveImg").attr("src",Global.imageServer + res.body[0].smallPath);
                $("#idCardPositiveImg").show("fast");
                $("#delIdCardPositiveImgBtn").show("fast");
                $("#delPhotoImgBtn").show("fast");
                $("#idCardPositive").val(res.body[0].sourcePath);
            }
            ,error: function(){
                //请求异常回调
                layer.msg("上传失败");
            }
        });

        // 上传身份证反面
        var idCardPositiveUploadBtn = upload.render({
            elem: '#idCardOtherSideUploadBtn' //绑定元素
            ,method : 'post'
            ,data : {path : 'iems'}
            ,url: Global.imageServer+'upload/images/' //上传接口
            ,done: function(res){
                //上传完毕回调
                $("#idCardOtherSideUploadBtn").hide();
                $("#idCardOtherSideImg").attr("src",Global.imageServer + res.body[0].smallPath);
                $("#idCardOtherSideImg").show("fast");
                $("#delIdCardOtherSideImgBtn").show("fast");
                $("#idCardOtherSide").val(res.body[0].sourcePath);
            }
            ,error: function(){
                //请求异常回调
                layer.msg("上传失败");
            }
        });
        // 删除上传的图片
        $("#delIdCardOtherSideImgBtn").click(function () {
            $("#idCardOtherSide").val("");
            $("#idCardOtherSideImg").removeAttr("src");
            $(this).hide("fast");
            $("#idCardOtherSideImg").hide("fast");

            $("#idCardOtherSideUploadBtn").show("fast");
        })

        // 删除上传的图片
        $("#delIdCardPositiveImgBtn").click(function () {
            $("#idCardPositive").val("");
            $("#idCardPositiveImg").removeAttr("src");
            $(this).hide("fast");
            $("#idCardPositiveImg").hide("fast");

            $("#idCardPositiveUploadBtn").show("fast");
        })

        // 删除上传的图片
        $("#delPhotoImgBtn").click(function () {
            $("#photo").val("");
            $("#photoImg").removeAttr("src");
            $(this).hide("fast");
            $("#photoImg").hide("fast");

            $("#photoUploadBtn").show("fast");
        })



        //自定义验证规则
        form.verify({
            isNumber: function(value) {
                if(value.length > 0 && !AM.isNumber.test(value)) {
                    return "请输入一个整数";
                }
            },
            isPhone: function(value) {
                if(value.length > 0 && !AM.isMobile.test(value)) {
                    return "请输入一个正确的手机号";
                }
            },
            isEmail: function(value) {
                if(value.length > 0 && !AM.isEmail.test(value)) {
                    return "请输入一个正确的邮箱";
                }
            },
            isNumberChar: function(value) {
                if(value.length > 0 && !AM.isNumberChar.test(value)) {
                    return "只能为数字和字母";
                }
            }

        });
        form.render();


        //监听提交
        form.on('submit(demo1)', function(data) {

            data.field.pwd = $.md5("111111");
            var roles = [];

            $("input[name='roles']:checked").each(function () {
                roles.push($(this).val());
            });

            if(roles.length == 0){
                layer.msg("没有选择角色");
                return false;
            }
            data.field.roles = roles.toString();
            data.field.birthday = new Date(data.field.birthday);
            data.field.induction = new Date(data.field.induction);
            console.log("提交参数")
            console.log(data.field)
            AM.ajaxRequestData("post", false, AM.ip + "/user/save", data.field  , function(result) {
                if (result.flag == 0 && result.code == 200) {
                    //关闭iframe页面
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                    window.parent.closeNodeIframe();
                } else {
                    layer.msg(result.msg, {icon: 2,anim: 6});
                    return false;
                }
            });
            return false;
        });
    });

    $("#homePhone").keyup(function(){
        if($("#homePhone").val().length > 11){
            $("#homePhone").val($("#homePhone").val().substring(0,$("#homePhone").val().length-1))
        }
    });
    $("#workPhone").keyup(function(){
        if($("#workPhone").val().length > 11){
            $("#workPhone").val($("#workPhone").val().substring(0,$("#workPhone").val().length-1))
        }
        $("#homePhone").val($("#workPhone").val());
    });

</script>
</body>
</html>
